package com.corpgovernment.common.utils;

import com.corpgovernment.common.apollo.FlightApollo;
import com.corpgovernment.common.bo.SupplierCodeClassification;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;

/**
 * @author yuxing zhang
 * @date 2024/4/8 18:57
 */
@Component
public class SupplierUtil {

    @Autowired
    private FlightApollo flightApollo;

    /**
     *
     * @param supplierCode 供应商code
     * @return true:携程美亚  false:接程曦API的供应商（Apollo配置） null:发生错误(供应商code不存在）
     */
    public Boolean judgeSupplierCodeBelongToCtripOrMeiYa(String supplierCode) {
        if (StringUtils.isBlank(supplierCode)){
            return null;
        }
        String supplierCodeClassification = flightApollo.getSupplierCodeClassification();
        if (StringUtils.isNotBlank(supplierCodeClassification)){
            SupplierCodeClassification codeClassification = JsonUtils.parse(supplierCodeClassification, new TypeReference<SupplierCodeClassification>() {
            });
            if (Objects.isNull(codeClassification)){
                return null;
            }
            HashSet<String> oldCategorySet = codeClassification.getOldCategorySet();
            HashSet<String> commonCategorySet = codeClassification.getCommonCategorySet();
            if (CollectionUtils.isNotEmpty(commonCategorySet) && commonCategorySet.contains(supplierCode)){
                //只有包含时才返回 false
                return false;
            }
            if (CollectionUtils.isNotEmpty(oldCategorySet) && oldCategorySet.contains(supplierCode)){
                //只有包含时才返回 true
                return true;
            }
        }
        return null;
    }

}
